package com.toweave.starter.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.toweave.starter.R;
import com.toweave.starter.components.ToastUtil;

public class SpinnerDialogActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

  // 定义一个数组，作为初始化选项
  private String[] mItems = {"北京", "上海", "广州", "深圳"};
  private Spinner spinner_dialog;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner_dialog);

    spinner_dialog = findViewById(R.id.spinner_dialog);

    // 声明一个下拉列表的数组适配器
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.item_select, mItems);
    spinner_dialog.setPrompt("请选择城市");
    spinner_dialog.setAdapter(adapter);
    // 设置默认选择第一项
    spinner_dialog.setSelection(0);
    // 设置监听事件
    spinner_dialog.setOnItemSelectedListener(this);


  }


  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Log.i("-liz-", "onItemSelected: " + position + "__" + mItems[position]);
    new ToastUtil(SpinnerDialogActivity.this, mItems[position]);
  }

  @Override
  public void onNothingSelected(AdapterView<?> parent) {

  }
}